package client;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JOptionPane;
import javax.swing.plaf.metal.MetalIconFactory;

import util.MyParser;
import util.Point;
import util.PointList;

public class GuiListener implements ActionListener, MouseMotionListener,
		MouseListener, KeyListener {
	private int currX = 0;
	private int currY = 0;
	private int prevX = 0;
	private int prevY = 0;
	private Graphics g;
	private PointList pl;
	private String message;
	private Socket socket;
	private OutputStream os;
	private PrintWriter out;
	private int chosenGame = 0;
	private boolean paintMaster = false;
	private String[] games = new String[10];

	public GuiListener(PointList pl, Socket socket) {
		this.pl = pl;
		this.socket = socket;
	}

	public void clearPointlist() {
		pl.clear();
	}

	public void setGames(String[] games) {
		this.games = games;
	}

	public void setpoints(String[] points) {
		StringBuilder sb = new StringBuilder();
		for (int n = 0; n < points.length - 1; n += 2) {
			sb.append(points[n] + ": " + points[n + 1] + "\n");

		}
		GUI.scoreBoard.setText((sb.toString()));

	}

	private void sendPaint() {
		message = "P" + MyParser.FIELD + GUI.getColor() + MyParser.FIELD
				+ MyParser.listToString(pl);
		try {
			os = socket.getOutputStream();
			out = new PrintWriter(os, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(message);
		out.flush();
	}

	public void sendGame(String s) {

		message = "G;;" + s;
		try {
			os = socket.getOutputStream();
			out = new PrintWriter(os, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(message);
		out.flush();
	}

	private void sendChat(String s) {
		message = "C;;" + s;
		try {
			os = socket.getOutputStream();
			out = new PrintWriter(os, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(message);
		out.flush();
	}

	public void actionPerformed(ActionEvent a) {
		if (a.getActionCommand().equals("red")) {
			GUI.setColor(Integer.toString(Color.red.getRGB()));
		}
		if (a.getActionCommand().equals("blue")) {
			GUI.setColor(Integer.toString(Color.blue.getRGB()));
		}
		if (a.getActionCommand().equals("green")) {
			GUI.setColor(Integer.toString(Color.green.getRGB()));
		}
		if (a.getActionCommand().equals("black")) {
			GUI.setColor(Integer.toString(Color.black.getRGB()));
		}
		if (a.getActionCommand().equals("exit")) {
			sendGame("exitgame");
			System.exit(1);
		}
		if (a.getActionCommand().equals("leavegame")) {
			sendGame("leavegame");
			g = GUI.sketchArea.getGraphics();
			g.setColor(GUI.sketchArea.getBackground());
			g.fillRect(5, 5, GUI.sketchArea.getWidth() - 10, GUI.sketchArea
					.getHeight() - 10);
		}
		if (a.getActionCommand().equals("new")) {

			int nbrPlayers = 0;
			int nbrRounds = 0;
			String s = JOptionPane.showInputDialog(GUI.sketchArea,
					"Choose number of players", "Players",
					JOptionPane.QUESTION_MESSAGE);
			if (s != null && !s.equals("")) {
				nbrPlayers = Integer.parseInt(s);
			}

			String s2 = JOptionPane.showInputDialog(GUI.sketchArea,
					"Choose number of rounds", "Rounds",
					JOptionPane.QUESTION_MESSAGE);
			if (s != null && !s.equals("")) {
				nbrRounds = Integer.parseInt(s2);
			}
			sendGame("newgame" + MyParser.FIELD + nbrPlayers + MyParser.DATA
					+ nbrRounds);
			GUI.chatHistory.setText("----------------");
		}
		if (a.getActionCommand().equals("start")) {
			sendGame("startgame");
			sendGame("clear");
		}
		if (a.getActionCommand().equals("join")) {

			sendGame("getgames");
			while (games[0] == null) {
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			if (games[0] != null) {
				String choice = (String) JOptionPane.showInputDialog(
						GUI.sketchArea, "Choose the game you wish to enter.",
						"Games", JOptionPane.QUESTION_MESSAGE, MetalIconFactory
								.getTreeComputerIcon(), games,
						"inget spel valt");
				if (choice != null && !choice.equals("nogames")) {

					sendGame("joingame" + MyParser.FIELD
							+ Integer.parseInt(choice));
					GUI.chatHistory.setText("----------------");
				}
			}
		}
		if (a.getActionCommand().equals("clear")) {
			g = GUI.sketchArea.getGraphics();
			g.setColor(GUI.sketchArea.getBackground());
			g.fillRect(5, 5, GUI.sketchArea.getWidth() - 10, GUI.sketchArea
					.getHeight() - 10);
			sendGame("clear");
		}
		if (a.getActionCommand().equals("getgames")) {
			sendGame("getgames");
		}
		if (a.getActionCommand().equals("send")) {
			String s = GUI.chatField.getText();
			if (!s.equals("")) {
				sendChat(s);
				GUI.chatField.setText("");
			}
		}
		if (a.getActionCommand().equals("about")) {
		}
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		g = GUI.sketchArea.getGraphics();
		currX = arg0.getX();
		currY = arg0.getY();
		if (currX < GUI.sketchArea.getWidth() - 5 && currX > 5
				&& currY < GUI.sketchArea.getHeight() - 5 && currY > 5) {
			pl.add(new Point(currX, currY));
			g.drawLine(prevX, prevY, currX, currY);
		}
		if (pl.size() > 10) {
			sendPaint();
			pl.clear();
			pl.add(new Point(prevX, prevY));
			pl.add(new Point(currX, currY));
		}
		prevX = currX;
		prevY = currY;

	}

	@Override
	public void mouseMoved(MouseEvent arg0) {

	}

	@Override
	public void mouseClicked(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
		prevX = e.getX();
		prevY = e.getY();
		pl.add(new Point(prevX, prevY));

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.getX() < GUI.sketchArea.getWidth()
				&& e.getY() < GUI.sketchArea.getHeight()) {
			pl.add(new Point(e.getX(), e.getY()));
		}
		if (!pl.isEmpty()) {
			sendPaint();
		}
		pl.clear();

	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
			String s = GUI.chatField.getText();

			if (!s.equals("")) {
				sendChat(s);
				GUI.chatField.setText("");
			}

		}

	}

	@Override
	public void keyReleased(KeyEvent arg0) {

	}

	@Override
	public void keyTyped(KeyEvent arg0) {

	}

}
